/**
  ******************************************************************************
  * @file    log.h
  * @author  zhujun
  * @version V1.0
  * @date    2016-04-22
  * @brief   串口输出, printf函数实现头文件
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2016 MindX</center></h2>
  ******************************************************************************
  */
/** @addtogroup BSP
* @{
*/
#ifndef _LOG_H_
#define _LOG_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stm32f10x.h>
#include <stdio.h>
    
#include "FreeRTOS.h"    
#include "task.h"
    
#define CONSOLE_BUF_SIZE            128         /**< 命令行缓冲区大小 */
#define CONSOLE_USART               USART1      /**< 调试用串口编号 */

/**Log level*/
typedef enum
{
    LOG_NONE,       /*!< No log output */
    LOG_ERROR,      /*!< Critical errors, software module can not recover on its own */
    LOG_WARN,       /*!< Error conditions from which recovery measures have been taken */
    LOG_INFO,       /*!< Information messages which describe normal flow of events */
    LOG_DEBUG,      /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
    LOG_VERBOSE     /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
} log_level_t;

#define  CONFIG_LOG_DEFAULT_LEVEL   LOG_DEBUG

#define LOG_COLOR_BLACK             "30"
#define LOG_COLOR_RED               "31"
#define LOG_COLOR_GREEN             "32"
#define LOG_COLOR_BROWN             "33"
#define LOG_COLOR_BLUE              "34"
#define LOG_COLOR_PURPLE            "35"
#define LOG_COLOR_CYAN              "36"
#define LOG_COLOR(COLOR)            "\033[0;" COLOR "m"
#define LOG_BOLD(COLOR)             "\033[1;" COLOR "m"
#define LOG_RESET_COLOR             "\033[0m"
#define LOG_COLOR_E                 LOG_COLOR(LOG_COLOR_RED)
#define LOG_COLOR_W                 LOG_COLOR(LOG_COLOR_BROWN)
#define LOG_COLOR_I                 LOG_COLOR(LOG_COLOR_GREEN)
#define LOG_COLOR_D
#define LOG_COLOR_V

#define Get_Log_Timestamp           xTaskGetTickCount
#define debug_printf                printf

#define LOG_FORMAT(letter, format)  LOG_COLOR_##letter "[%08d ms] %s(%d): " format LOG_RESET_COLOR "\r\n"

#ifndef LOG_LOCAL_LEVEL
#define LOG_LOCAL_LEVEL             ((log_level_t) CONFIG_LOG_DEFAULT_LEVEL)
#endif

#define debug_logE(format, ...)  if (LOG_LOCAL_LEVEL >= LOG_ERROR)   { debug_printf(LOG_FORMAT(E, format), Get_Log_Timestamp(), __FILE__, __LINE__, ##__VA_ARGS__); }
#define debug_logW(format, ...)  if (LOG_LOCAL_LEVEL >= LOG_WARN)    { debug_printf(LOG_FORMAT(W, format), Get_Log_Timestamp(), __FILE__, __LINE__,##__VA_ARGS__); }
#define debug_logI(format, ...)  if (LOG_LOCAL_LEVEL >= LOG_INFO)    { debug_printf(LOG_FORMAT(I, format), Get_Log_Timestamp(), __FILE__, __LINE__,##__VA_ARGS__); }
#define debug_logD(format, ...)  if (LOG_LOCAL_LEVEL >= LOG_DEBUG)   { debug_printf(LOG_FORMAT(D, format), Get_Log_Timestamp(), __FILE__, __LINE__,##__VA_ARGS__); }
#define debug_logV(format, ...)  if (LOG_LOCAL_LEVEL >= LOG_VERBOSE) { debug_printf(LOG_FORMAT(V, format), Get_Log_Timestamp(), __FILE__, __LINE__,##__VA_ARGS__); }
#define HEX_PRINT(buffer, len)   if (LOG_LOCAL_LEVEL >= LOG_VERBOSE) { hexPrint(buffer, len);}


#ifdef __cplusplus
}
#endif
    
#endif /*_PRINTF_H_ */

/**
  * @}
  */
/******************* (C) COPYRIGHT 2017 MINDX ******END OF FILE******************/

